'use strict';

goog.provide('Blockly.Arduino.customs');

goog.require('Blockly.Arduino');


Blockly.Arduino.customs_digital_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'number_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.customs_analog_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.customs_show_highlow = function() {
  var dropdown_d_value = Blockly.Arduino.valueToCode(this, 'd_values', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};



Blockly.Arduino.customs_digital_write = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  var value_p_value = Blockly.Arduino.valueToCode(this, 'p_value', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Arduino.setups_['setup_output_' + value_pin] = 'pinMode(' + value_pin + ', OUTPUT);';
  var code = 'digitalWrite(' + value_pin + ', ' + value_p_value + ');\n';
  return code;
};


Blockly.Arduino.customs_digital_read = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Arduino.setups_['setup_output_' + value_pin] = 'pinMode(' + value_pin + ', INPUT);';
  var code = 'digitalRead(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_NONE];
};

Blockly.Arduino.customs_analog_write = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  var value_p_value = Blockly.Arduino.valueToCode(this, 'p_value', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Arduino.setups_['setup_output_' + value_pin] = 'pinMode(' + value_pin + ', OUTPUT);';
  var code = 'analogWrite(' + value_pin + ', ' + value_p_value + ');\n';
  return code;
};


Blockly.Arduino.customs_analog_read = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  Blockly.Arduino.setups_['setup_output_' + value_pin] = 'pinMode(' + value_pin + ', INPUT);';
  var code = 'analogRead(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.customs_pulse_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Roller_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Key_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Action_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Photosensitive_triggering_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Magnetic_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Touch_switch_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Threshold_trigger_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_potentiometer_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Pressure_sensing_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Photosensitive_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Sound_sensor_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Grayscale_detection_module_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Remote_sensing_module_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Ultrasonic_range_finder_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Sliding_rheostat_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};

Blockly.Arduino.customs_Infrared_detector_pin = function() {
  // Numeric value.
  var code = Blockly.Arduino.valueToCode(this, 'string_values', Blockly.Arduino.ORDER_ATOMIC);
  // -4.abs() returns -4 in Dart due to strange order of operation choices.
  // -4 is actually an operator and a number.  Reflect this in the order.
  var order = code < 0 ?
      Blockly.Arduino.ORDER_UNARY_PREFIX : Blockly.Arduino.ORDER_ATOMIC;
  return [code, order];
};


Blockly.Arduino.customs_motor_m = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.customs_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.customs_servo_motor_single = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
  var dropdown_motor_m = Blockly.Arduino.valueToCode(this, 'MOTOR_M', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_direction_set = Blockly.Arduino.valueToCode(this, 'DIRECTION_SET', Blockly.Arduino.ORDER_ATOMIC);
  var number_speed_set_val = Blockly.Arduino.valueToCode(this, 'SPEED_SET_VAL', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var PIN_list=[];
    var min_speed=0;  //其中最小的输出
    if(dropdown_motor_m=='M1'){// 第一个电机
        if(dropdown_direction_set=='F'){  //正转
            PIN_list[0]='5';
            PIN_list[1]='3';
        }
        else{
            PIN_list[0]='3';
            PIN_list[1]='5';
        }
    }
    else{
        if(dropdown_direction_set=='F') {  //正转
            PIN_list[0] = '6';
            PIN_list[1] = '9';
        }
        else{
            PIN_list[0] = '9';
            PIN_list[1] = '6';
        }
    }
    Blockly.Arduino.setups_['setup_output'+PIN_list[0]] = 'pinMode('+PIN_list[0]+', OUTPUT);';
    Blockly.Arduino.setups_['setup_output'+PIN_list[1]] = 'pinMode('+PIN_list[1]+', OUTPUT);';
    var code = 'analogWrite(' + PIN_list[0] + ', ' + number_speed_set_val + ');\n';
    code=code+'analogWrite(' + PIN_list[1] + ', ' + min_speed + ');\n';
  return code;
};


Blockly.Arduino.customs_two_motor_direction = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.customs_two_motor_speed = function() {
  var dropdown_d_value = this.getFieldValue('text_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_d_value;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.customs_servo_motor_two = function() {
    //正转 D5>D3 D9>D6
    //反转D3>D5  D6>D9
    //D5 D3电机在左边
    //D9 D6电机在右边
  var dropdown_motor_m = Blockly.Arduino.valueToCode(this, 'MOTOR_M', Blockly.Arduino.ORDER_ATOMIC);
  var dropdown_speed_set = Blockly.Arduino.valueToCode(this, 'SPEED_SET', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    var pin_list=['5','3','9','6'];
    var pin_value=[];
    var init_pin_value='0';
    switch(dropdown_motor_m){
        case 'F':
            pin_value=[dropdown_speed_set,init_pin_value,dropdown_speed_set,init_pin_value];
            break;
        case 'B':
            pin_value=[init_pin_value,dropdown_speed_set,init_pin_value,dropdown_speed_set];
            break;
        case 'L':
            var min_set=0;
            pin_value=[dropdown_speed_set,init_pin_value,min_set,init_pin_value];
            break;
        case 'R':
            var min_set=0;
            pin_value=[min_set,init_pin_value,dropdown_speed_set,init_pin_value];
            break;
    }
    var code='';
    for(var i=0;i<pin_list.length;i++){
        Blockly.Arduino.setups_['setup_output'+pin_list[i]] = 'pinMode('+pin_list[i]+', OUTPUT);';
        code=code+'analogWrite(' + pin_list[i] + ', ' + pin_value[i] + ');\n';
    }
  return code;
};



function oled_one_row_post_data(text,x,y) {
      var post_data={'text':text,'x':x,'y':y};
      var hex_list=[];
      $.ajax({
        url: "http://"+window.location.host+"/oled_display/",
        type: "Post",
        async: false,
        data: JSON.stringify(post_data),
        dataType: "json",
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert("error!" + errorThrown);

        },
        success: function (data) {
          // var parse_data=JSON.parse(data);
          var hex_data=data['data'];
          if(hex_data.length>=3){
            hex_list=hex_data;
          }
        }
      });
      return hex_list;
    }

function oled_multiple_row_post_data(text_list) {
  var hex_list=[];
  var post_data={"text_list":text_list};
      $.ajax({
        url: "http://"+window.location.host+"/oled_multiple_display/",
        type: "Post",
        async: false,
        data: JSON.stringify(post_data),
        dataType: "json",
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          alert("error!" + errorThrown);

        },
        success: function (data) {
          // var parse_data=JSON.parse(data);
          var hex_data=data['data'];
          if(hex_data.length>=3){
            hex_list=hex_data;
          }
        }
      });
      return hex_list;
}

function stringHasUnicode(str){
  //判断是否含有unicode字符
  for(var i = 0; i < str.length; i++){
      var val = str.charCodeAt(i);
      if (val>127){  //ascii编码的值小于或等于127
        return true;
      }
  }
  return false;
}


Blockly.Arduino.customs_oled_single = function() {
  var value_display_text = Blockly.Arduino.valueToCode(this, 'DISPLAY_TEXT', Blockly.Arduino.ORDER_ATOMIC);
  var value_row_x = Blockly.Arduino.valueToCode(this, 'ROW_X', Blockly.Arduino.ORDER_ATOMIC);
  var value_column_y = Blockly.Arduino.valueToCode(this, 'COLUMN_Y', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    Blockly.Arduino.definitions_['define_U8glib'] = '#include <U8glib.h>\n';
    Blockly.Arduino.definitions_['define_U8GLIB_SH1106_128X64'] = 'U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);\n';
    Blockly.Arduino.setups_['setRot180']='u8g.setRot180();\n';
    var code='';
    var func_name=' OLED_display_single'+Math.round(Math.pow(Math.random(),3)*100000000);
    var code_start='void '+func_name+'(int x,int y)\n{\nu8g.firstPage();\ndo {\n';
    var code_end='}\nwhile ( u8g.nextPage() );\n}\n';
    var text_mark='789showtext';   //文本显示标记
    if(value_display_text==null || value_display_text=='') {
      value_display_text=''
    }
    else if(value_display_text.length>=11 && value_display_text.substr(0,11)==text_mark){
      if(stringHasUnicode(value_display_text.substr(11,value_display_text.length))){
        var hex_list=oled_one_row_post_data(value_display_text.substr(11,value_display_text.length),value_row_x,value_column_y);
        var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
        Blockly.Arduino.definitions_[range_name]=hex_list[0]+"\n";
        var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
        Blockly.Arduino.definitions_[range_name]=code_start+hex_list[3]+code_end;
        code=func_name+'('+value_row_x+','+value_column_y+');'+"\n";
      }
      else{
        var single_draw = 'void single_draw(int x,int y,char * text) {\nu8g.setFont(u8g_font_unifont);\nu8g.drawStr( x, y, text);\n}\n';
        Blockly.Arduino.definitions_['define_single_draw'] =single_draw;
        var func_n='char single_text[]='+value_display_text.substr(11,value_display_text.length)+';\nsingle_draw(x,y,single_text);\n';
        var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
        Blockly.Arduino.definitions_[range_name]=code_start+func_n+code_end;
        code=func_name+"("+value_row_x+","+value_column_y+"+10);\n"
      }
    }
    else {
      var single_draw = 'void single_draw(int x,int y,char * text) {\nu8g.setFont(u8g_font_unifont);\nu8g.drawStr( x, y, text);\n}\n';
      Blockly.Arduino.definitions_['define_single_draw'] =single_draw;
        var func_n='char text[]="";\nsprintf(text,"%d",'+value_display_text+');\nsingle_draw(x,y,text);\n';
        var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
        Blockly.Arduino.definitions_[range_name]=code_start+func_n+code_end;
        code=func_name+"("+value_row_x+","+value_column_y+"+10);\n"
    }

  return code;
};


Blockly.Arduino.customs_oled_display = function() {
  var value_text_one = Blockly.Arduino.valueToCode(this, 'TEXT_ONE', Blockly.Arduino.ORDER_ATOMIC);
  var value_text_two = Blockly.Arduino.valueToCode(this, 'TEXT_TWO', Blockly.Arduino.ORDER_ATOMIC);
  var value_text_three = Blockly.Arduino.valueToCode(this, 'TEXT_THREE', Blockly.Arduino.ORDER_ATOMIC);
  var value_text_four = Blockly.Arduino.valueToCode(this, 'TEXT_FOUR', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    Blockly.Arduino.definitions_['define_U8glib'] = '#include <U8glib.h>\n';
    Blockly.Arduino.definitions_['define_U8GLIB_SH1106_128X64'] = 'U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);\n';
    Blockly.Arduino.setups_['setRot180']='u8g.setRot180();\n';
    var text_mark='789showtext';   //文本显示标记
    var func_name=' OLED_display_multiple'+Math.round(Math.pow(Math.random(),3)*100000000);
    var code_start='void '+func_name+'(int x,int y)\n{\nu8g.firstPage();\ndo {\n';
    var code_end='}\nwhile ( u8g.nextPage() );\n}\n';
    if(value_text_one==null || value_text_one==''){
      value_text_one=""
    }
    if(value_text_two==null || value_text_two==''){
      value_text_two=""
    }
    if(value_text_three==null || value_text_three==''){
      value_text_three=""
    }
    if(value_text_four==null || value_text_four==''){
      value_text_four=""
    }

    var post_one="";
    var post_two="";
    var post_three="";
    var post_four="";
    if(value_text_one.length>=11 && value_text_one.substr(0,11)==text_mark){
      post_one=value_text_one.substr(11,value_text_one.length);
    }
    else{
      post_one=value_text_one;
    }

    if(value_text_two.length>=11 && value_text_two.substr(0,11)==text_mark){
      post_two=value_text_two.substr(11,value_text_two.length);
    }
    else{
      post_two=value_text_two;
    }

    if(value_text_three.length>=11 && value_text_three.substr(0,11)==text_mark){
      post_three=value_text_three.substr(11,value_text_three.length);
    }
    else{
      post_three=value_text_three;
    }

    if(value_text_four.length>=11 && value_text_four.substr(0,11)==text_mark){
      post_four=value_text_four.substr(11,value_text_four.length);
    }
    else{
      post_four=value_text_four;
    }
    var hex_list=oled_multiple_row_post_data([post_one,post_two,post_three,post_four]);
    var code='';
    var line_list=[];
    var single_draw = 'void single_draw(int x,int y,char * text) {\nu8g.setFont(u8g_font_unifont);\nu8g.drawStr( x, y, text);\n}\n'
    Blockly.Arduino.definitions_['define_single_draw'] =single_draw;
    if(value_text_one.length>=11 && value_text_one.substr(0,11)==text_mark){
      if(stringHasUnicode(post_one)){
        line_list[0]=hex_list[3][0];
      }else{
        var func_1='char text_1[]='+post_one+';\nsingle_draw(0,16,text_1);\n';
        line_list[0]=func_1;
      }

    }
    else{
        var func_1='char text_1[]="";\nsprintf(text_1,"%d",'+value_text_one+');\nsingle_draw(0,16,text_1);\n';
        if(value_text_one!=null && value_text_one!=''){
           line_list[0]=func_1;
        }
        else{
         line_list[0]="";
        }

    }
    if(value_text_two.length>=11 && value_text_two.substr(0,11)==text_mark){
      if(stringHasUnicode(post_two)){
         line_list[1]=hex_list[3][1];
      }
      else{
         var func_2='char text_2[]='+post_two+';\nsingle_draw(0,32,text_2);\n';
        line_list[1]=func_2;
      }
    }
    else{
        var func_2='char text_2[]="";\nsprintf(text_2,"%d",'+value_text_two+');\nsingle_draw(0,32,text_2);\n';
         if(value_text_two!=null && value_text_two!=''){
           line_list[1]=func_2;
        }
        else{
         line_list[1]="";
        }
    }
    if(value_text_three.length>=11 && value_text_three.substr(0,11)==text_mark){
      if(stringHasUnicode(post_three)){
         line_list[2]=hex_list[3][2];
      }
     else {
         var func_3='char text_3[]='+post_three+';\nsingle_draw(0,48,text_3);\n';
          line_list[2]=func_3;
      }
    }
    else{
         var func_3='char text_3[]="";\nsprintf(text_3,"%d",'+value_text_three+');\nsingle_draw(0,48,text_3);\n';
       if(value_text_three!=null && value_text_three!=''){
           line_list[2]=func_3;
        }
        else{
         line_list[2]="";
        }
    }
    if(value_text_four.length>=11 && value_text_four.substr(0,11)==text_mark){
      if(stringHasUnicode(post_four)){
        line_list[3]=hex_list[3][3];
      }
      else {
        var func_4='char text_4[]='+post_four+';\nsingle_draw(0,64,text_4);\n';
        line_list[3]=func_4;
      }

    }
    else{
        var func_4='char text_4[]="";\nsprintf(text_4,"%d",'+value_text_four+');\nsingle_draw(0,64,text_4);\n';
         if(value_text_four!=null && value_text_four!=''){
           line_list[3]=func_4;
        }
        else{
         line_list[3]="";
        }
    }
    var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
    Blockly.Arduino.definitions_[range_name]=hex_list[0];
      // Blockly.Arduino.definitions_['define_single_draw_func']=hex_list[1];
    var draw_text=line_list.join("");
    var range_name = 'draw'+Math.round(Math.pow(Math.random(),3)*100000000);
    Blockly.Arduino.definitions_[range_name]=code_start+draw_text+code_end;
    code=func_name+'(0,0);'+"\n";
  return code;
};


Blockly.Arduino.customs_tone_rhythm = function() {
  var dropdown_rhythm = this.getFieldValue('number_values');
  // TODO: Assemble JavaScript into code variable.
  var code = dropdown_rhythm;
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

Blockly.Arduino.customs_play_sound = function() {
  var value_tone_play = Blockly.Arduino.valueToCode(this, 'tone_play', Blockly.Arduino.ORDER_ATOMIC);
  var value_frequency = Blockly.Arduino.valueToCode(this, 'frequency', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
    Blockly.Arduino.setups_['setup_output_'+value_tone_play] = 'pinMode('+value_tone_play+', OUTPUT);';
  var code = '';
    code=code+'tone('+value_tone_play+','+value_frequency+');\n';
  return code;
};


Blockly.Arduino.customs_play_sound_duration = function() {
  var value_tone_play = Blockly.Arduino.valueToCode(this, 'tone_play', Blockly.Arduino.ORDER_ATOMIC);
  var value_frequency = Blockly.Arduino.valueToCode(this, 'frequency', Blockly.Arduino.ORDER_ATOMIC);
  var value_otime = Blockly.Arduino.valueToCode(this, 'otime', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
   Blockly.Arduino.setups_['setup_output_'+value_tone_play] = 'pinMode('+value_tone_play+', OUTPUT);';
    var code='';
    code=code+'tone('+value_tone_play+','+value_frequency+','+value_otime+'*1000);\ndelay('+value_otime+'*1000);\n';
  return code;
};



Blockly.Arduino.customs_stop_sound = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
  var code = '';
    code=code+'noTone('+value_pin+');\n';
  return code;
};


Blockly.Arduino.customs_temperature_read = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
 Blockly.Arduino.definitions_['define_Dht11'] = '#include <dht11.h>\n';
 Blockly.Arduino.definitions_['define_dht11_obj'] = 'dht11 dht11_obj;\n';
 Blockly.Arduino.definitions_['define_temperature_read']='int temperature_read(int pin){\n int code=dht11_obj.read(pin);\n return dht11_obj.temperature;\n}\n';
  var code = 'temperature_read(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_NONE];
};


Blockly.Arduino.customs_humidity_read = function() {
  var value_pin = Blockly.Arduino.valueToCode(this, 'pin', Blockly.Arduino.ORDER_ATOMIC);
  // TODO: Assemble JavaScript into code variable.
 Blockly.Arduino.definitions_['define_Dht11'] = '#include <dht11.h>\n';
 Blockly.Arduino.definitions_['define_dht11_obj'] = 'dht11 dht11_obj;\n';
 Blockly.Arduino.definitions_['define_humidity_read']='int humidity_read(int pin){\n int code=dht11_obj.read(pin);\n return dht11_obj.humidity;\n}\n';
  var code = 'humidity_read(' + value_pin + ')';
  // TODO: Change ORDER_NONE to the correct strength.
  return [code, Blockly.Arduino.ORDER_NONE];
};
